package com.zg.common.alter.scheduler;

import com.zg.common.alter.autoconfigure.AlertProperties;
import com.zg.common.alter.interceptor.AlertInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * <p>Description: </p>
 *
 * @author zhanfeng.liang
 * @since 2025/2/11 16:54
 */
//@Component
@RequiredArgsConstructor
public class AlertScheduler {
    private final AlertInterceptor interceptor;
    private final AlertProperties alertProperties;
    //private final AlertService alertService;

    @Scheduled(fixedRate = 60000) // 每分钟检查一次
    public void checkErrorRates() {
        Map<String, Integer> errors = interceptor.getErrorCounts();
        errors.forEach((path, count) -> {
            if (count > alertProperties.getErrorThreshold()) {
                //alertService.sendAlert(path, "错误次数超标：" + count);
            }
        });
        interceptor.resetErrorCounts(); // 重置计数器
    }
}
